package test.myorm.handler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import test.myorm.core.Sql;

public class StatementHandler {
	
	public PreparedStatement handle(Sql sql, Connection conn) throws SQLException {
		int returnIdType = sql.getReturnIdType();
		PreparedStatement ps = null;
		if(returnIdType==Sql.ID_GENERATE) {
			ps = conn.prepareStatement(sql.getSql(), Statement.RETURN_GENERATED_KEYS);
		} else {
			ps = conn.prepareStatement(sql.getSql());
		}
		List<Object> params = sql.getParameter().getParamList();
		if(CollectionUtils.isNotEmpty(params)) {
			for(int i=0;i<params.size();i++) {
				ps.setObject(i + 1, params.get(i));
			}
		}
		
		return ps;
	}
}
